package com.rongshu.view.submitArticle.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.rongshu.R;
import com.rongshu.view.base.AdapterListener;
import com.rongshu.view.userInfo.entity.Article;
import com.utils.SPUtils;
import com.utils.constants.SPKey;

import java.util.List;

public class SelectFloderAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context mContext = null;

    private List<Article.ResponseBean> articles;
    private AdapterListener listener;

    public SelectFloderAdapter(Context context, List<Article.ResponseBean> articles) {
        this.mContext = context;
        this.articles = articles;
    }

    public void setAdapter(AdapterListener listener) {
        this.listener = listener;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_select_floder, parent, false);
        return new ArticleViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        final Article.ResponseBean article = articles.get(position);
        ArticleViewHolder viewHolder = (ArticleViewHolder) holder;
        viewHolder.txt_article.setText(article.getTitle());
        int floder_id = SPUtils.getPreferences(SPKey.FLODER_ID, 0);
        if (floder_id != 0) {
            viewHolder.selectIcon.setVisibility(floder_id == articles.get(position).getId() ? View.VISIBLE : View.INVISIBLE);
        } else {
            viewHolder.selectIcon.setVisibility(position == 0 ? View.VISIBLE : View.INVISIBLE);
        }
        viewHolder.layout_article.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SPUtils.putPreferences(SPKey.FLODER_ID, articles.get(position).getId());
                notifyDataSetChanged();
                if (listener != null) {
                    listener.callBack(article);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        if (articles == null) {
            return 0;
        }
        return articles.size();
    }

    public static class ArticleViewHolder extends RecyclerView.ViewHolder {
        private RelativeLayout layout_article;
        private TextView txt_article;
        private ImageView selectIcon;

        public ArticleViewHolder(View itemView) {
            super(itemView);
            layout_article = itemView.findViewById(R.id.layout_article);
            txt_article = itemView.findViewById(R.id.txt_article);
            selectIcon = itemView.findViewById(R.id.selclet_icon);
        }
    }

}
